//获得用户登录名
function getQueryVariable(variable)
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){return pair[1];}
    }
    return(false);
}

//页面内容更换
function disp(id){

    if(id=='manage')
    {
        var element = document.getElementById('cmanage');
        element.style.visibility = "visible";
        element.style.display = "block";

        var element = document.getElementById('cadd');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cdelete');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cedit');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cinquiry');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('ccategory');
        element.style.visibility = "hidden";
        element.style.display = "none";
    }
    if(id=='categoryadd')
    {
        var element = document.getElementById('ccategory');
        element.style.visibility = "visible";
        element.style.display = "block";

        var element = document.getElementById('cadd');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cdelete');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cedit');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cinquiry');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cmanage');
        element.style.visibility = "hidden";
        element.style.display = "none";

    }

    if(id=='inquiry')
    {
        var element = document.getElementById('cinquiry');
        element.style.visibility = "visible";
        element.style.display = "block";

        var element = document.getElementById('cadd');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cdelete');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cedit');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cmanage');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('ccategory');
        element.style.visibility = "hidden";
        element.style.display = "none";
    }
    if(id=='add')
    {
        var element = document.getElementById('cadd');
        element.style.visibility = "visible";
        element.style.display = "block";

        var element = document.getElementById('cinquiry');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cdelete');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cedit');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cmanage');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('ccategory');
        element.style.visibility = "hidden";
        element.style.display = "none";
    }
    if(id=='delete')
    {
        var element = document.getElementById('cdelete');
        element.style.visibility = "visible";
        element.style.display = "block";

        var element1 = document.getElementById('cadd');
        element1.style.visibility = "hidden";
        element1.style.display = "none";

        var element2 = document.getElementById('cinquiry');
        element2.style.visibility = "hidden";
        element2.style.display = "none";

        var element3 = document.getElementById('cedit');
        element3.style.visibility = "hidden";
        element3.style.display = "none";

        var element = document.getElementById('cmanage');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('ccategory');
        element.style.visibility = "hidden";
        element.style.display = "none";
    }
    if(id=='edit')
    {
        var element = document.getElementById('cedit');
        element.style.visibility = "visible";
        element.style.display = "block";

        var element = document.getElementById('cadd');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cdelete');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cinquiry');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('cmanage');
        element.style.visibility = "hidden";
        element.style.display = "none";

        var element = document.getElementById('ccategory');
        element.style.visibility = "hidden";
        element.style.display = "none";
    }

}



window.onload=function()
{
    var username=getQueryVariable("un");
    document.getElementById("welcome").innerHTML=username;
    $.ajax({
        type:"GET",
        dataType: "json",
        contentType: "application/json",
        url:"/commodity/getAll",
        data: {

        },
        success:function(data){
            var wzy='';
            document.getElementById('tinquiry').innerHTML = "";
            for(var i=0;i<data.length;i++){
                var j=i+1;
                wzy += "<tr>" +"<th>" + j+ "</th>"+ "<th>" + data[i].commodity.name + "</th>" + "<th>" +data[i].category.name+ "</th>" + "<td>" + data[i].commodity.price + "</td>" + "<td>"+ data[i].commodity.inventory + "</td>" + "</tr>";
            }
            $("#tinquiry").after(wzy);
        },
        error: function () {
            alert("查询失败");
        }
    })
    $.ajax({
        type:"GET",
        dataType: "json",
        contentType: "application/json",
        url:"/commodity/getAll",
        data: {

        },
        success:function(data){
            var wzy='';
            document.getElementById('tdelete').innerHTML = "";
            for(var i=0;i<data.length;i++){
                var j=i+1;
                wzy += "<tr>" + "<th>" + j+ "</th>"+"<td>" + "<input  type=\"radio\" name=\"personality\" id='" + data[i].commodity.id + "'>" + "</td>" +"<th>" + data[i].commodity.name + "</th>" + "<th>" +data[i].category.name+ "</th>" + "<td>" + data[i].commodity.price + "</td>" + "<td>"+ data[i].commodity.inventory + "</td>" +"<td>"+ data[i].commodity.sale + "</td>" + "</tr>";
            }
            $("#tdelete").after(wzy);
        },
        error: function () {
            alert("查询失败");
        }
    })
    $.ajax({
        type:"GET",
        dataType: "json",
        contentType: "application/json",
        url:"/commodity/getAll",
        data: {

        },
        success:function(data){
            var wzy='';
            document.getElementById('tedit').innerHTML = "";
            for(var i=0;i<data.length;i++){
                var j=i+1;
                wzy += "<tr>" + "<th>" + j+ "</th>"+"<td>" + "<input  type=\"radio\" name=\"personality\" id='" + data[i].commodity.id + "' >" + "</td>" +"<th contentEditable='true'>" + data[i].commodity.name + "</th>" + "<th contentEditable='true'>" +data[i].category.name+ "</th>" + "<td contentEditable='true'>" + data[i].commodity.price + "</td>" + "<td contentEditable='true'>"+ data[i].commodity.inventory + "</td>" +"<td contentEditable='true'>"+ data[i].commodity.sale + "</td>" + "</tr>";
            }
            $("#tedit").after(wzy);
        },
        error: function () {
            alert("查询失败");
        }
    })

    $.ajax({
        type:"GET",
        dataType: "json",
        contentType: "application/json",
        url:"/category/getAll",
        data: {

        },
        success:function(data){
            var wzy='';
            document.getElementById('tmanage').innerHTML = "";
            for(var i=0;i<data.length;i++){
                var j=i+1;
                wzy += "<tr>" + "<th>" + j+ "</th>" +"<td contentEditable='true'>"+ data[i].name + "</td>";
                wzy +="<th>"+"<a style='cursor: pointer;'"+"onclick='correct("+data[i].id+","+i+")'"+">"+"修改"+"</a>"+"|"+"<a style='cursor: pointer;'"+"onclick='del("+data[i].id+")'"+">"+"删除"+"</a>"+"</th>" + "</tr>";
            }
            $("#tmanage").after(wzy);
        },
        error: function () {
            alert("查询失败");
        }
    })
}
function correct(id,i)
{
    i=i+1;
    console.log(id);
    console.log(i);
    var mytable = document.getElementById('tablemanage');
    var str = mytable.rows[i].cells[1].innerHTML;
    var va= {
        "id": id,
        "name": str
    };
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json",
        url: "/category/addOrUpdate",
        data: JSON.stringify(va),
        success: function (data) {
            alert('修改成功！');
            location.reload();
        },
        error: function () {
            alert("修改失败");
        }
    })
}
function del(id)
{
    console.log(id);
    $.ajax({
        type: "GET",
        dataType: "json",
        contentType: "application/json",
        url: "/category/deleteById",
        data: {
            "categoryId": Number(id),
        },
        success: function (data) {
            alert('删除成功！');
            location.reload();
        },
        error: function () {
            alert("删除失败");
        }
    })
}
function madd() {
    var name=document.getElementById("cname").value;
    console.log(name);
    var i=0;
    var va={
        "id": i,
        "name": name
    };
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json",
        url: "/category/addOrUpdate",
        data:JSON.stringify(va),
        success: function (data) {
            alert('添加成功！');
            location.reload();
        },
        error: function () {
            alert("添加失败");
        }
    })
}
function del1()
{
    var biao=0;
    var nodes = document.getElementsByTagName("input");
    var s = 0;
    for(var i=0;i<nodes.length;i++)
    {
        if(nodes[i].type=="radio" && nodes[i].checked) {
            s = parseInt(nodes[i].id);
            biao=1;
        }
    }
    if(biao==0)
    {
        alert('请勾选')
    }
    else {
        $.ajax({
            type: "GET",
            dataType: "json",
            contentType: "application/json",
            url: "/commodity/deleteById",
            data: {
                "commodityId": s
            },
            success: function (data) {
                alert('删除成功！');
                location.reload();
            },
            error: function () {
                alert("删除失败");
            }
        })
    }
}

function edit1()
{
    var nodes = document.getElementsByTagName("input");
    var biao=0;
    var s;
    var nodes = document.getElementsByTagName("input");
    for(var i=0;i<nodes.length;i++)
    {
        if(nodes[i].type=="radio" && nodes[i].checked) {
            s = parseInt(nodes[i].id);
            biao=1;
        }
    }
    if(biao==0)
    {
        alert('请勾选')
    }
    else {
        var mytable = document.getElementById('tableedit');
        for (var i = 1, rows = mytable.rows.length; i < rows; i++) {
            if (mytable.rows[i].cells[1].getElementsByTagName("INPUT")[0].id == s) {
                var str = mytable.rows[i].cells[2].innerHTML;
                var name = str;
                var category = mytable.rows[i].cells[3].innerHTML;
                var price = mytable.rows[i].cells[4].innerHTML;
                var inventory = mytable.rows[i].cells[5].innerHTML;
            }
        }
        var teacher = {
            "category": {
                "id": 0,
                "name": category
            },
            "commodity": {
                "id": s,
                "inventory": inventory,
                "name": name,
                "price": price,
                "sale": 25,
                "url": null
            }
        };
        $.ajax({
            type: "POST",
            dataType: "json",
            contentType: "application/json",
            url: "/commodity/addOrUpdate",
            data: JSON.stringify(teacher),
            success: function (data) {
                alert("保存成功");
                location.reload();
            },
            error: function () {
                alert("保存失败");
            }
        })
    }
}

//添加商品页面的操作
function add()
{
    var name=document.getElementById("name").innerHTML;
    var category=document.getElementById("category").innerHTML;
    var price=document.getElementById("price").innerHTML;
    var inventory=document.getElementById("inventory").innerHTML;

    var teacher={
        "category": {
            "id": 0,
            "name": category
        },
        "commodity": {
            "id": 0,
            "inventory":inventory,
            "name": name,
            "price": price,
            "sale": 25,
            "url": null
        }
    };
    $.ajax({
        type:"POST",
        dataType: "json",
        contentType: "application/json",
        url:"/commodity/addOrUpdate",
        data: JSON.stringify(teacher) ,
        success:function(data){
            alert("添加成功");
            location.reload();
        },
        error: function () {
            alert("添加失败");
        }
    })
}

function exit()
{
    $.ajax({
        type:"GET",
        dataType: "json",
        contentType: "application/json",
        url:"/admin/logout",
        success:function(data){
        },
        error: function () {
            alert("退出失败");
        }
    })
}